#!/usr/bin/perl

# $Id: getdkp.pl,v 1.15 2008/01/11 11:21:57 dmitry Exp $

use LWP::Simple;

@pages = (
	{ title => "Gruul's Lair",	 	url => 'http://dkp.volgocity.ru/gruul/listmembers.php?show=all' },
	{ title => "Tempest Keep",	 	url => 'http://dkp.volgocity.ru/tk/listmembers.php?show=all' },
);

$|=1;
$i=0;

open F,">dkp.lua";
print F "-- DKP Table generated ".localtime(time)."\n\n";
print F "DKP_tables = {}\n\n";

foreach $pg (@pages) {
	$title=${$pg}{title};
	$url=${$pg}{url};
	print "Retriving ".(++$i).".$title \[$url\]...";
	if($page=get($url)) {
		$j=1;
		undef @table;
		print "OK\n";
		print "Parsing...";
		$page=~s/^.+<form method=\"post\" action=\"listmembers.php\?s=[^<]+<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">//s;
		$page=~s/^.+?<tr>.+?<\/tr>//s;
		while($page=~/<tr.*?>.*?<td.*?>.+?<\/td>.*?<td.*?>.+?<\/td>.*?<td.*?><a.*?>(.+?)<\/a><\/td>.*?<td.*?>(.+?)<\/td>.*?<td.*?>.+?<\/td>.*?<td.*?>(.+?)<\/td>.*?<td.*?>.+?<\/td>.*?<td.*?>.+?<\/td>.*?<td.*?>.+?<\/td>.*?<td.*?>(.+?)<\/td>.*?<td.*?>.+?<\/td>.*?<\/tr>/sg) {
	    	($nick,$rank,$class,$value)=($1,$2,$3,$4);
		    $nick=~s/<.*?>//g;
		    $class=lc($class);
		    $rank=~s/<.*?>//g;
		    $rank='' if $rank eq '&nbsp;';
	    	push @table,"{ nick=w2u(\"$nick\"), rank=\"$rank\", class=DKPT_$class, current=$value, spent=0 }";
			print $j++."..";
		}
		$j--;
		print "Done.\n\n";
		print F "DKP_tables\[$i\] = {\n\t";
		print F join(",\n\t",@table);
		print F "\n};\n";
		print F "DKP_tables\[$i\].title = \"$title\";\n";
		print F "DKP_tables\[$i\].url = \"$url\";\n";
		print F "DKP_tables\[$i\].members = $j;\n\n";
	} else {
		print "hren\n";
	}
}
print F "DKP_tables_count = $i\n";
print F "DKP_tables_timestamp = \"".localtime(time)."\"\n";

close F;
